﻿using System;
using System.Linq;
using System.Web.Mvc;
using CinemaNetwork.Models;

namespace CinemaNetwork.Controllers
{
    public class AdminController : Controller
    {
        private CinemaEntities db = new CinemaEntities();
        public ActionResult Index()
        {
            var cinemas = (from cinema in db.cinemas select cinema).ToList();
            return View(cinemas);
        }

        public ActionResult ViewCinema(int id)
        {
            var films = (from FilmByCinema in db.FilmByCinemas
                         where FilmByCinema.cinemaId == id
                         select FilmByCinema).ToList();
            return View(films);
        }

        public ActionResult ViewSession(int filmId, int cinemaId)
        {
            var sessions = (from SessionByFilmAndCinema in db.SessionByFilmAndCinemas
                            where SessionByFilmAndCinema.cinemaId == cinemaId
                            where SessionByFilmAndCinema.filmId == filmId
                            select SessionByFilmAndCinema).ToList();
            return View(sessions);
        }

        public ActionResult Create()
        {
            session seanse = new session();
            return View(seanse);
        } 

        [HttpPost]
        public ActionResult Create(session seanse)
        {
            try
            {
                if (ModelState.IsValid) 
                {
                    db.AddTosessions(seanse);
                    db.SaveChanges();
                    return RedirectToAction("Home/Index");
                }

            }
            catch (Exception ex)
            {
                ModelState.AddModelError(String.Empty, ex);
            }
            return View(seanse);
        }
        
        public ActionResult Delete(int id)
        {
            return View();
        }

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
